<?php
/**
 * Created by PhpStorm.
 * User: 刘抱
 * Date: 2018/12/29
 * Time: 14:20
 */

namespace App\Controller;

//use Symfony\Component\HttpFoundation\Response;
use App\Db\lib\FileCache;
use App\Model\User;
use App\Service\UserService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class LuckyController extends AbstractController
{
    /**
     * @Route("lucky/number")
     */
    public function number()
    {
        $number = random_int(0, 100);
        return $this->render('lucky/number.html.twig',[
            'number' => $number,
        ]);
    }

    /**
     * @Route("test")
     */
    public function test()
    {
        $fileCache = new FileCache('/tmp/cache'); //设置文件缓存路径
        $result = $fileCache->get('test');
        if (!$result){
            //使用原生sql方法
            $em = $this->getDoctrine();
            $conn = $em->getConnection();
            $sql = "select * from api.user";
//        $query = $conn->prepare($sql);
//        $query->execute();
//        $result = $query->fetchAll();
            $result = $conn->fetchAll($sql);
            $fileCache->set('test',$result,10); //存缓存 key,value,秒数
            $result = $fileCache->get('test');
        }
        print_r($result);die; //读缓存
    }

    /**
     * //@Route("/user/login",name="user", methods={"POST"})
     */
    public function login(Request $request): Request
    {
        $data = json_decode($request->getContent(), true);
        /*exit(\Doctrine\Common\Util\Debug::dump($data));

        return new JsonResponse(
            [
                'status' => 'ok',
            ],
            JsonResponse::HTTP_CREATED
        );*/
        /*$product = $this->getDoctrine()
            ->getRepository(Product::class)
            ->find($data['username']);*/

        print_r($data);die;

    }

    /**
     * @Route("model")
     */
    public function model()
    {
        $userModel = $this->userModel();
        $res = $userModel->testSelect();
        print_r($res);die;

    }

    protected function userModel()
    {
        return new User();
    }

}
